#pragma once
#include <string>
#include <vector>

#include"Skill.h"
#include "Equipment.h"
 
#include "json.hpp"

using json = nlohmann::json;

class Monster;
class Character
{
protected:
	std::string characterName;
	std::string characterInfo;

	int currentHealth;
	int maxHealth;
	int baseAttack; // 基础攻击，装备加成会额外计算
	int baseDefense;
	int mp;
	int maxMP;
	int defeatedMonstersCount;

	std::vector<Skill> skills; // 当前拥有的技能
	std::vector<Equipment> equipments; // 当前装备

	bool m_fromSave = false;

    virtual void uniqueEffect()
    {

    }

	virtual void initialize() {}

public:
	Character(std::string name, std::string info, int h, int at, int de,int mp);
    virtual ~Character() = default;

	void isLoad(bool value);
	void initCharacter() { initialize(); }
    virtual Character* clone() const = 0;
	virtual std::string getType() const = 0;
    bool stun=false;

	virtual void Attack(Monster& monster);
	void UseSkill(int index, Monster& monster);
	void OnTakeDamage(int value);
	void OnDie();
    virtual void EndTurn();

	bool addEquipment(const Equipment& equipment);
	void unequipEquipment(int index);
	bool addSkill(const Skill& skill);

	void replaceEquipment(int index, const Equipment& newEquipment);
	void replaceSkill(int index, const Skill& newSkill);


    void setBaseAttack(int value);
    void setBaseDefense(int value);
    void setCurrentHealth(int value);
	void setCurrentMP(int value);

	void RecoilState();
	
	std::string getName() const;
	std::string getInfo() const;
	int getCurrentHealth() const;
	int getMaxHealth() const;
	int getDefense() const;
	int getMP() const;
	int getMaxMP() const;
	int getTotalAttack() const;
	void updateSkillList();

	void defeatMonster();
	int getDefeatedCount() const;

	void showSkills() const;
    void showEquipments() const;
    
    // 获取装备和技能列表
    const std::vector<Equipment>& getEquipment() const { return equipments; }
    const std::vector<Skill>& getSkills() const { return skills; }
    

    void applyEquipmentBonuses();

    // 序列化为JSON
	json toJson() const;
	// 从 JSON 构建对象，返回具体派生实例
	static Character* fromJson(const json& j);
};